OutputTrait.php
<?php
namespace Tlf\Tester;
trait OutputTrait {
public function cliOutput($testSet){
ob_start();
echo "\n";
$testCount = [];
$totalPass = 0;
$totalFail = 0;
$testNameToExpand = $this->answers['test'][0] ?? null;
$testsToExpand = [];
foreach ($testSet as $dir=>$classes){
echo "dir $dir\n";
foreach($classes as $class=>$info){
echo "class $class\n";
if (count($info['tests'])==0)echo " No Tests";
ob_start();
foreach ($info['tests'] as $testName => $result){
if ($testName[0]=='_')continue;
echo " ";
$r = $result;
if ($r['disabled']){
echo "//";
} else if ($r['result']){
echo "+";
$totalPass++;
} else {
$totalFail++;
echo "-";
}
$testName = substr($testName,strlen('test'));
if ($testName==$testNameToExpand){
$testsToExpand[] = $result;
}
echo " $testName";
echo "\n";
}
}
}
echo "\n";
if (count($testsToExpand)>0){
echo "\n\nHighlighted Tests: \n";
foreach ($testsToExpand as $t){
$passStr = $t['error']==''? ($t['result'] ? 'pass' : 'fail') : 'error';
echo " # ".$t['method'].": $passStr\n";
$out = explode("\n", $t['output']);
$indent = " ";
$out = $indent.implode("\n$indent",$out);
echo $out."\n\n";
if ($passStr=='error'){
$error = (array)$t['error'];
// echo var_export($error, true);
$errorString = $error["\0Error\0string"] ?? $error["\0Exception\0string"];
$errorString = explode("\n", $errorString);
$errorString = $indent.implode("\n$indent", $errorString);
echo $errorString;
}
}
}
echo "Passing: $totalPass tests";
echo "\nFailing: $totalFail tests";
echo "\n";
return ob_get_clean();
}
public function htmlOutput($testSet){
ob_start();
$testCount = [];
$testDir = realpath($this->pwd);
foreach ($testSet as $dir=>$classes){
$dir = realpath($dir);
$relDir = $dir;
$prefix = substr($dir,0,strlen($testDir));
if ($prefix===$testDir)$relDir = substr($dir,strlen($testDir)+1);
$pad = " ";
echo "<h1>Dir: $relDir</h1>\n";
echo "$pad<section>";
// echo "Tests in '$dir'\n";
foreach($classes as $class=>$info){
echo "$pad$pad<h2>$class</h2>";
foreach ($info['tests'] as $testName => $result){
if (is_int($result))continue;
echo $result['html'];
// echo " ";
// $r = $result;
// if ($r['disabled'])echo "//";
// else if ($r['result'])echo "+";
// else echo "-";
// echo " $testName";
// echo "\n";
}
}
echo "$pad</section>";
}
$htmlOut = ob_get_clean();
return $htmlOut;
}
public function writeTestResults($html){
if (!$this->answers['results.writeHtml']){
return;
}
$outFile = $this->dir.'/TestResults.html';
file_put_contents($outFile, $html);
$outFile = realpath($outFile);
echo "\nSee file://$outFile for extended output";
}
}